package com.canbot.u05.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;

import com.canbot.u05.utils.ConstUtils;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.Utils;

import java.util.ArrayList;

public class FrameAnimationFromSD extends FrameAnimation {

        private static final String TAG = "FrameAnimationFromSD";

        private ArrayList<String> MapPathStr;

        public FrameAnimationFromSD(Context context) {
                super(context, null);
        }

        public FrameAnimationFromSD(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
        }

        public FrameAnimationFromSD(Context context, AttributeSet attrs) {
                super(context, attrs, 0);
        }

        public void drawView() {
                Logger.v(TAG, "drawView--11");
                // 无资源文件退出
                if (MapPathStr == null) {
                        Log.e("frameview", "the bitmapsrcIDs is null");

                        mIsThreadRunning = false;

                        return;
                }
                Logger.v(TAG, "drawView--22");
                // 锁定画布
                mCanvas = mSurfaceHolder.lockCanvas();
                try {
                        if (mSurfaceHolder != null && mCanvas != null) {

                                mCanvas.drawColor(Color.BLACK);
                                Logger.v(TAG, "drawView--33");
                                // 如果图片过大可以再次对图片进行二次采样缩放处理
                                Logger.v(TAG, "drawView--44" + MapPathStr.get(mCurrentIndext));
                                mBitmap = Utils.fromSD(ConstUtils.SD_Path_Image_XQ + MapPathStr.get(mCurrentIndext));
                                Logger.v(TAG, "drawView--66" + mBitmap.toString());
                                mCanvas.drawBitmap(mBitmap, (getWidth() - mBitmap.getWidth()) / 2,
                                        (getHeight() - mBitmap.getHeight()) / 2, null);

                                // 播放到最后一张图片，停止线程
                                if (mCurrentIndext == MapPathStr.size() - 1 && !restart) {
                                        mIsThreadRunning = false;
                                }

                        }
                }
                catch (Exception e) {
                        e.printStackTrace();
                }
                finally {
                        if (mCurrentIndext == MapPathStr.size() - 1)
                                mCurrentIndext = 0;
                        else
                                mCurrentIndext++;

                        if (mCanvas != null) {
                                // 将画布解锁并显示在屏幕上
                                mSurfaceHolder.unlockCanvasAndPost(mCanvas);
                        }

                        if (mBitmap != null) {
                                // 收回图片
                                mBitmap.recycle();
                        }
                }
        }

        boolean restart = true;

        public void setRestart(boolean res) {
                this.restart = res;
        }

        public void setMapPath(ArrayList<String> MapPathStr) {
                this.MapPathStr = MapPathStr;
        }

}
